<?php

use exphp\RequestNetwork;

/**
 * 发送GET请求
 * @param string $url 请求地址
 * @param array $param 请求参数
 * @param integer $port 端口号
 * @return void
 */
function exGetRequest($url, $param = array(), $port = 80)
{
    RequestNetwork::setGetRequestInfo($url, $param, $port);
    RequestNetwork::execute();
    if (RequestNetwork::$response['Error']) {
        exErrorMessage('get request failed', RequestNetwork::$response['Error'], 500);
    }
    return RequestNetwork::$response['Content'];
}

/**
 * 发送POST请求
 * @param string $url 请求地址
 * @param array $body 请求参数
 * @param integer $port 端口号
 * @return void
 */
function exPostRequest($url, $body = array(), $port = 80)
{
    RequestNetwork::setPostRequestInfo($url, $body, $port);
    RequestNetwork::execute();
    if (RequestNetwork::$response['Error']) {
        exErrorMessage('get request failed', RequestNetwork::$response['Error'], 500);
    }
    return RequestNetwork::$response['Content'];
}

/**
 * 发送Put请求
 * @param string $url 请求地址
 * @param array $body 请求参数
 * @param integer $port 端口号
 * @return void
 */
function exPutRequest($url, $body = array(), $port = 80)
{
    RequestNetwork::setPutRequestInfo($url, $body, $port);
    RequestNetwork::execute();
    if (RequestNetwork::$response['Error']) {
        exErrorMessage('get request failed', RequestNetwork::$response['Error'], 500);
    }
    return RequestNetwork::$response['Content'];
}

/**
 * 发送PATCH请求
 * @param string $url 请求地址
 * @param array $body 请求参数
 * @param integer $port 端口号
 * @return void
 */
function exPatchRequest($url, $body = array(), $port = 80)
{
    RequestNetwork::setPatchRequestInfo($url, $body, $port);
    RequestNetwork::execute();
    if (RequestNetwork::$response['Error']) {
        exErrorMessage('get request failed', RequestNetwork::$response['Error'], 500);
    }
    return RequestNetwork::$response['Content'];
}

/**
 * 发送DELETE请求
 * @param string $url 请求地址
 * @param array $body 请求参数
 * @param integer $port 端口号
 * @return void
 */
function exDeleteRequest($url, $body = array(), $port = 80)
{
    RequestNetwork::setDeleteRequestInfo($url, $body, $port);
    RequestNetwork::execute();
    if (RequestNetwork::$response['Error']) {
        exErrorMessage('get request failed', RequestNetwork::$response['Error'], 500);
    }
    return RequestNetwork::$response['Content'];
}

/**
 * 设置Header头,属于追加，多次设置将合并，需要在请求前设置
 * @param array $headers 请求头，支持格式['XXX:XXX','XXX:XXX']
 * @return void
 */
function exSetHeaders($headers)
{
    return RequestNetwork::setHeaders($headers);
}

/**
 * 设置Cookie值,属于追加，多次设置将合并，需要在请求前设置
 * @param string $cookie
 * @return void
 */
function exSetCookie($cookie)
{
    return RequestNetwork::setCookie($cookie);
}

/**
 * 设置Referer值,属于追加，多次设置将合并，需要在请求前设置
 * @param string $referer 
 * @return void
 */
function exSetReferer($referer)
{
    return RequestNetwork::setReferer($referer);
}


/**
 * 设置SSL信息
 * @param boolean $isOpen 是否开启SSL
 * @param [type] $cert 携带证书
 * @param [type] $key 携带密钥
 * @return void
 */
function exSetSSL($isOpen = true, $cert = null, $key = null)
{
    return RequestNetwork::setSSL($isOpen, $cert, $key);
}
